package edu.bu.cs673.superkids123.children.controller.games;

import java.util.List;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.children.DataObjectAccess.LessonGameManager;
import edu.bu.cs673.superkids123.children.controller.AbstractGameController;
import edu.bu.cs673.superkids123.children.model.ArithmaticQuestion;
import edu.bu.cs673.superkids123.children.model.Game;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.view.games.FanFairView;
import edu.bu.cs673.superkids123.children.view.games.GameResultsView;
import edu.bu.cs673.superkids123.children.view.games.ArithmaticGameView;
import edu.bu.cs673.superkids123.common.model.Child;
/**
 * NumberMatchingGameView
 * 
 * @author Joshua Stein
 *
 */
public class ArithmaticGameController extends  AbstractGameController {

	private final int NUMBER_OF_QUESTIONS = 10;
	private int currentQuestionNumber;
	public ArithmaticQuestion[] questions = new ArithmaticQuestion[NUMBER_OF_QUESTIONS];
	public ArithmaticQuestion currentQuestion;
	private boolean isBusy = false;
	private GameResultsController resultsController;
	
	public ArithmaticGameController(AbstractFrame mainFrame) {
		super(mainFrame);
		resultsController = getMainFrame().getController( GameResultsController.class );
	}
	public void startGame(){
		resultsController.notifyResultsController("Arithmatic Game");
		buildQuestionSet();
		currentQuestionNumber = 0;
		refreshCurrentQuestion();
		getMainFrame().changeView(ArithmaticGameView.class);
		getMainFrame().getView(ArithmaticGameView.class).setBackground();
		
		// Start Stat Recording
		List<Game> games = LessonGameManager.getAllActiveGames();
		for (Game game : games) {
			if (game.getName().contains("Click"))
				start(game);
		}
	}
	
	public void endGame(boolean complete) {
		if (isActive()) {
			int score = resultsController.getCorrect() * 10;
			stop(complete, score);
		}

		if (complete) {
       		getMainFrame().getController(GameResultsController.class)
       			.showResultsScreen(this.getEarnedAchievements());
		}
	}
	
	private void buildQuestionSet(){ 
		int age = ( (Child)authController.getCurrentUser()).getAge();
		int numOptions = 3;
		for(int i=0; i<NUMBER_OF_QUESTIONS; i++){
			questions[i] = new ArithmaticQuestion(age,numOptions);
		}
	}
	private void refreshCurrentQuestion(){
		currentQuestion = questions[currentQuestionNumber];
	}
	public void nextQuestion(){
		if( currentQuestionNumber < (NUMBER_OF_QUESTIONS-1) ) {
			currentQuestionNumber++;
			refreshCurrentQuestion(); 
		}
		else{
			// End Game
			endGame(true);
//			getMainFrame().changeView(GameResultsView.class);
		}
	}
	public void answerButtonPressed(ArithmaticGameView view, String optionNum) {
		if (isBusy) 
			return;
		isBusy = true;
		processSelection( view, Integer.parseInt(optionNum) );
	}
	
	public void processSelection(final ArithmaticGameView view, int optionNum) {
		
		final int chosenAnswerLocation = optionNum;
		
		Thread processSelectionThread = new Thread() {
			public void run() {
				int correctAnswerLocation = questions[currentQuestionNumber].getCorrectAnswerLocation();
				//Correct Answer
				if (chosenAnswerLocation == correctAnswerLocation) {
					resultsController.correct++;
					
					getMainFrame().changeView(FanFairView.class);
					view.playAudio(ChildrenResourceProvider.getInstance().getAudioContent(ChildrenResource.audioCorrect));
					getMainFrame().changeView(ArithmaticGameView.class);
				    if (currentQuestionNumber <= 10) {
				    	nextQuestion();
				    	view.tryAgainOff();
				    }
				    else {
				    	isBusy = false;
				    }
				}
				//Wrong Answer
				else {
					resultsController.wrong++;
					view.tryAgainOn();
					try {
						Thread.sleep(500);
					}
					catch (InterruptedException ie) {};
				}
				isBusy = false;
			}
		};
		processSelectionThread.start();
	}
}
